<?php

/**
 * Description de la classe MEMBER
 */
class Member {
    // constante globale à l'application WSK PORTAL

    const USER = 0;
    const FULLADMIN = 1;
    const USER_ENABLE = 1;
    const USER_DISABLE = 0;

    private $id;
    private $login;
    private $password;
    private $email;
    private $first_name;
    private $last_name;
    private $address;
    private $company;
    private $city;
    private $zip;
    private $phone_number;
    private $avatar;
    private $statut;
    private $is_enabled;

    /**
     *
     * @param type $id
     * @param type $login
     * @param type $email
     * @param type $first_name
     * @param type $last_name
     * @param type $address
     * @param type $company
     * @param type $city
     * @param type $zip
     * @param type $phone_number
     * @param type $avatar
     * @param type $statut
     * @param type $is_enabled 
     */
    function __construct($id, $password, $login, $email, $first_name, $last_name, $address, $company, $city, $zip, $phone_number, $avatar, $statut, $is_enabled) {
        $this->id = $id;
        $this->password = $password;
        $this->login = $login;
        $this->email = $email;
        $this->first_name = $first_name;
        $this->last_name = $last_name;
        $this->address = $address;
        $this->company = $company;
        $this->city = $city;
        $this->zip = $zip;
        $this->phone_number = $phone_number;
        $this->avatar = $avatar;
        $this->statut = $statut;
        $this->is_enabled = $is_enabled;
    }

    public function getId() {
        return $this->id;
    }

    public function getLogin() {
        return $this->login;
    }

    public function getEmail() {
        return $this->email;
    }

    public function getFirst_name() {
        return $this->first_name;
    }

    public function getLast_name() {
        return $this->last_name;
    }

    public function getAddress() {
        return $this->address;
    }

    public function getCompany() {
        return $this->company;
    }

    public function getCity() {
        return $this->city;
    }

    public function getZip() {
        return $this->zip;
    }

    public function getPhone_number() {
        return $this->phone_number;
    }

    public function getAvatar() {
        return $this->avatar;
    }

    public function getStatut() {
        return $this->statut;
    }

    public function getIs_enabled() {
        return $this->is_enabled;
    }

    /**
     * Fonction d'enregistrement d'un membre
     */
    public function save() {

        Sql::exec("INSERT INTO `wsk_members` (`id_member`, `login`, `password`, `email`, `first_name`, `last_name`, `address`, `company`, `city`, `zip`, `phone_number`, `avatar`, `statut`, `is_enabled`, `date_created`, `date_updated`, `date_deleted`) VALUES
('null', '" . $this->login . "', '" . $this->password . "', '" . $this->email . "', '" . $this->first_name . "', '" . $this->last_name . "', '" . $this->address . "', '" . $this->company . "', '" . $this->city . "', '" . $this->zip . "', '" . $this->phone_number . "', NULL, " . $this->statut . ", '" . $this->is_enabled . "', NULL, NULL, NULL)
");
       Log::createLog("INSERT INTO `wsk_members` (`id_member`, `login`, `password`, `email`, `first_name`, `last_name`, `address`, `company`, `city`, `zip`, `phone_number`, `avatar`, `statut`, `is_enabled`, `date_created`, `date_updated`, `date_deleted`) VALUES
('null', '" . $this->login . "', '" . $this->password . "', '" . $this->email . "', '" . $this->first_name . "', '" . $this->last_name . "', '" . $this->address . "', '" . $this->company . "', '" . $this->city . "', '" . $this->zip . "', '" . $this->phone_number . "', NULL, " . $this->statut . ", '" . $this->is_enabled . "', NULL, NULL, NULL)
"); 
    }

    /**
     * Mise à jour d'un membre
     */
    public function update() {
        if ($this->password == null) {
            Sql::exec("UPDATE  `wsk_members` SET  `login` =  '" . $this->login . "',
            `email` =  '" . $this->email . "',
            `first_name` =  '" . $this->first_name . "',
            `last_name` =  '" . $this->last_name . "',
            `address` =  '" . $this->address . "',
            `company` =  '" . $this->company . "',
            `city` =  '" . $this->city . "',
            `zip` =  '" . $this->zip . "',
            `phone_number` =  '" . $this->phone_number . "',
            `date_updated` = NOW(),
            `is_enabled` = '" . $this->is_enabled . "',
            `avatar` =  '" . $this->avatar . "' WHERE  `id_member` =" . $this->id . ";");
        } else {
            Sql::exec("UPDATE  `wsk_members` SET  `login` =  '" . $this->login . "',
            `password` =  '" . $this->password . "',
            `email` =  '" . $this->email . "',
            `first_name` =  '" . $this->first_name . "',
            `last_name` =  '" . $this->last_name . "',
            `address` =  '" . $this->address . "',
            `company` =  '" . $this->company . "',
            `city` =  '" . $this->city . "',
            `zip` =  '" . $this->zip . "',
            `phone_number` =  '" . $this->phone_number . "',
            `date_updated` = NOW(),
            `is_enabled` = '" . $this->is_enabled . "',
            `avatar` =  '" . $this->avatar . "' WHERE  `id_member` =" . $this->id . ";");
        }
    }

    // fonction de mise à jour d'un mot de passe
    public function updatePassword($password) {
        $this->password = $password;
        Sql::exec("UPDATE  `wsk_members` SET `password` = '" . $this->password . "',
    	 `date_updated` = NOW() WHERE  `id_member` =" . $this->id . ";");
    }

    /**
     * Suppresion d'un membre
     * Suppression logique
     */
    public function delete() {
        Sql::exec("UPDATE  `wsk_members` SET  `date_deleted` = NOW(), `is_enabled` = " . Member::USER_DISABLE . " WHERE `id_member` =" . $this->id . ";");
    }

    /**
     * Récupère l'id de l'utilisateur connecté
     * @param type $login
     * @param type $password
     * @return type 
     */
    public static function getIdAuthentify($login, $password) {

        $req = mysql_query("SELECT id_member FROM wsk_members WHERE login='" . $login . "' and password='" . $password . "' AND date_deleted IS NULL AND is_enabled = 1");
        $result = -1;
        if (mysql_num_rows($req) == 1) {
            $row = mysql_fetch_array($req);
            $result = $row["id_member"];
        }
        mysql_free_result($req);
        return $result;
    }

    /**
     * Récupère tous les membres 
     * getMembers
     * @return Author 
     */
    public static function getMembers() {

        $list = Array();

        $req = mysql_query("SELECT * FROM  `wsk_members` WHERE date_deleted IS NULL ORDER BY `is_enabled` ASC");
        while ($row = mysql_fetch_array($req)) {
            $list[] = new Member($row['id_member'], 'null', $row['login'], $row['email'], $row['first_name'], $row['last_name'], $row['address'], $row['company'], $row['city'], $row['zip'], $row['phone_number'], $row['avatar'], $row['statut'], $row['is_enabled']);
        }

        return $list;
    }

    /**
     * Retourne les administrateurs d'un utilisateur
     * @param type $member
     * @return \Member
     */
    public static function getAdminsOfMember($member) {
        $list = Array();

        $req = mysql_query("SELECT * FROM wsk_members 
            JOIN members_has_projects ON wsk_members.id_member = wsk_members_has_projects.id_member
            WHERE wsk_members_has_projects.user_type = 1
            AND wsk_members_has_projects.id_project IN 
            (
                SELECT id_project FROM wsk_members_has_projects 
                WHERE id_member = " . $member->getId() . " AND user_type=0 
            )");
        while ($row = mysql_fetch_array($req)) {
            $list[] = new Member($row['id_member'], 'null', $row['login'], $row['email'], $row['first_name'], $row['last_name'], $row['address'], $row['company'], $row['city'], $row['zip'], $row['phone_number'], $row['avatar'], $row['statut'], $row['is_enabled']);
        }

        return $list;
    }

    /**
     * Récupère un utilisateur par son ID unique
     * @param type $id_member
     * @return \Member
     */
    public static function getMemberById($id_member) {

        $req = mysql_query("SELECT * FROM wsk_members WHERE id_member='" . $id_member . "'");
        $row = mysql_fetch_array($req);
        mysql_free_result($req);
        return new Member($row['id_member'], 'null', $row['login'], $row['email'], $row['first_name'], $row['last_name'], $row['address'], $row['company'], $row['city'], $row['zip'], $row['phone_number'], $row['avatar'], $row['statut'], $row['is_enabled']);
    }

    /**
     * Récupère un membre par son login ou son email
     * 
     * @param type $loginOrEmail
     * @return \Member|null
     */
    public static function getMemberByLoginOrEmail($loginOrEmail) {
        $req = mysql_query("SELECT * FROM wsk_members WHERE login='" . $loginOrEmail . "' OR email='" . $loginOrEmail . "'");
        if (mysql_num_rows($req) == 1) {
            $row = mysql_fetch_array($req);
            mysql_free_result($req);
            return new Member($row['id_member'], 'null', $row['login'], $row['email'], $row['first_name'], $row['last_name'], $row['address'], $row['company'], $row['city'], $row['zip'], $row['phone_number'], $row['avatar'], $row['statut'], $row['is_enabled']);
        }

        mysql_free_result($req);
        return null;
    }

    /**
     * Fonction de génération de mot de passe aléatoire
     * @param type $length
     * @param type $strength
     * @return string
     */
    public static function generatePassword($length = 9, $strength = 0) {
        $vowels = 'aeuy';
        $consonants = 'bdghjmnpqrstvz';
        if ($strength & 1) {
            $consonants .= 'BDGHJLMNPQRSTVWXZ';
        }
        if ($strength & 2) {
            $vowels .= "AEUY";
        }
        if ($strength & 4) {
            $consonants .= '23456789';
        }
        if ($strength & 8) {
            $consonants .= '@#$%';
        }

        $password = '';
        $alt = time() % 2;
        for ($i = 0; $i < $length; $i++) {
            if ($alt == 1) {
                $password .= $consonants[(rand() % strlen($consonants))];
                $alt = 0;
            } else {
                $password .= $vowels[(rand() % strlen($vowels))];
                $alt = 1;
            }
        }
        return $password;
    }

    /**
     * Fonction permettant de vérifier la disponibilité d'un login
     * @param type $name
     * @return int
     */
    public static function checkAvailability($name) {
        $result = mysql_query('SELECT login FROM wsk_members WHERE login = "' . $name . '"');
        Log::createLog("Member::checkAvailability - start");

        Log::createLog('SELECT login FROM wsk_members WHERE login = "' . $name . '"');
        //Non disponible si supérieur à 0  
        if (mysql_num_rows($result) > 0) {
            Log::createLog("Member::checkAvailability - 0");
            //and we send 0 to the ajax request  
            return 0;
        } else {
            Log::createLog("Member::checkAvailability - 1");
            // Login disponible
            return 1;
        }
    }

}

?>